import { createRouter, createWebHistory } from 'vue-router'
import WebView from '../views/webViews/web.vue'
import AgreementCn from '@/views/agreementViews/agreement-cn.vue'
import AgreementUs from '@/views/agreementViews/agreement-us.vue'
import SignView from '../views/signViews/sign.vue'
import MainView from '../views/userViews/main.vue'
import UploadPage from '@/pages/userPages/uploadPage.vue'
import HistoryPage from '@/pages/userPages/historyPage.vue'
import ChatPage from '@/pages/userPages/chatPage.vue'
import BuyPage from '@/pages/userPages/buyPage.vue'
import VipLevelPage from '@/pages/userPages/buyPage1.vue'
import GiftPage from '@/pages/userPages/buyPage2.vue'
import SettingsPage from '@/pages/userPages/settingsPage.vue'
import GeneralPage from '@/pages/userPages/settingsGeneral.vue'
import InfoPage from '@/pages/userPages/settingsInfo.vue'
import PasswordPage from '@/pages/userPages/settingsPassword.vue'
import LogoutPage from '@/pages/userPages/settingsLogout.vue'
import PayView from '../views/payViews/pay.vue'
import DetailView from '@/views/detailViews/detail.vue'
import { useAuthStore } from '@/stores/auth'
import { useUploadStore } from '@/stores/upload'

import UploadTest from '../views/detailViews/uploadTest.vue'

const requireAuth = ['/main', '/main/*', '/pay']  // 需要登录后才能访问的页面
const routes = [
  {
    path: '/web',
    name: 'web',
    component: WebView,
  },
  {
    path: '/agreement-CN',
    name: 'acn',
    component: AgreementCn
  },
  {
    path: '/agreement-US',
    name: 'aus',
    component: AgreementUs
  },
  {
    path: '/sign',
    name: 'sign',
    component: SignView
  },
  {
    path: '/main',
    name: 'main',
    component: MainView,
    redirect: '/main/upload',
    children: [
      { path: 'upload', name: 'upload', component: UploadPage },
      { path: 'history', name: 'history', component: HistoryPage },
      { path: 'chat', name: 'chat', component: ChatPage },
      {
        path: 'buy',
        name: 'buy',
        component: BuyPage,
        redirect: '/main/buy/viplevel',
        children: [
          { path: 'viplevel', name: 'viplevel', component: VipLevelPage },
          { path: 'gift', name: 'gift', component: GiftPage }
        ]
      },
      {
        path: 'settings',
        name: 'settings',
        redirect: '/main/settings/general',
        component: SettingsPage,
        children: [
          { path: 'general', name: 'general', component: GeneralPage },
          { path: 'info', name: 'info', component: InfoPage },
          { path: 'password', name: 'password', component: PasswordPage },
          { path: 'logout', name: 'logout', component: LogoutPage }
        ]
      }
    ]
  },
  {
    path: '/pay',
    name: 'pay',
    component: PayView
  },
  {
    path: '/detail/:id',
    name: 'detail',
    component: DetailView,
  },
  {
    path: '/uploadTest',
    name: 'uploadTest',
    component: UploadTest
  },
  {
    path: '/',
    redirect: '/web'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (!to.path.startsWith('/main')) {
    const uploadStore = useUploadStore()
    uploadStore.clear()
  }
  const authStore = useAuthStore();
  if (to.path === '/sign' && from.path.startsWith('/main')) {
    authStore.setLogin(false);
  }
  const isLogin = authStore.isLogin;
  const needAuth = requireAuth.some(authPath => {
    if (authPath.endsWith('/*')) {
      const base = authPath.replace('/*', '');
      return to.path === base || to.path.startsWith(base + '/');
    }
    return to.path === authPath;
  });
  if (needAuth) {
    if (!isLogin) {
      next('/sign');
    } else {
      next();
    }
  } else {
    next();
  }
})

export default router